package com.ems.service.impl;

import com.ems.entity.PcsEntity;
import com.ems.mapper.PcsMapper;
import com.ems.service.IAlarmService;
import com.ems.service.IPcsAlarmBitService;
import com.ems.service.IPcsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ems.util.SnowflakeIdWorker;
import com.ems.util.StateEnum;
import com.ems.util.StateException;
import com.ems.vo.AlarmVo;
import com.ems.vo.PcsAlarmBitVo;
import com.ems.vo.PcsVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * PCS数据 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-04-23
 */
@Service
public class PcsServiceImpl extends ServiceImpl<PcsMapper, PcsEntity> implements IPcsService {

    @Resource
    private IAlarmService iAlarmService;
    @Resource
    private IPcsAlarmBitService iPcsAlarmBitService;
    @Resource
    private PcsMapper pcsMapper;

    @Transactional ( rollbackFor = Exception.class, propagation = Propagation.REQUIRED )
    @Override
    public PcsVo saveSingle ( PcsVo pcsVo ) {
        if ( pcsVo == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        PcsEntity pcsEntity = new PcsEntity ();
        BeanUtils.copyProperties ( pcsVo, pcsEntity );
        pcsEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        pcsEntity.setCreateTime ( new Date () );
        if ( pcsVo.check () ) {
            int insert = baseMapper.insert ( pcsEntity );
            if ( insert == 0 ) {
                throw new StateException ( StateEnum.INSERT_EXCE );
            }
        }
        if ( pcsVo.getAlarmVoList () != null && pcsVo.getAlarmVoList ().size () > 0 ) {
            for ( AlarmVo alarmVo : pcsVo.getAlarmVoList () ) {
                iAlarmService.saveSingle ( alarmVo );
            }
        }
        if ( pcsVo.getPcsAlarmBitVoList () != null && pcsVo.getPcsAlarmBitVoList ().size () > 0 ) {
            for ( PcsAlarmBitVo pcsAlarmBitVo : pcsVo.getPcsAlarmBitVoList () ) {
                if ( StringUtils.isNotBlank ( pcsAlarmBitVo.getAlarmValue () ) ) {
                    iPcsAlarmBitService.saveSingle ( pcsAlarmBitVo );
                }
            }
        }
        return pcsVo;
    }

    @Override
    public PcsVo querySingle ( PcsVo pcsVo ) {

        return null;
    }

    @Override
    public List<PcsVo> queryList ( PcsVo pcsVo ) {
        List<PcsVo> pcsVos = pcsMapper.queryList ( pcsVo );
        for ( PcsVo vo : pcsVos ) {
            PcsAlarmBitVo pcsAlarmBitVo = new PcsAlarmBitVo ( vo.getDeviceAddress () );
            List<PcsAlarmBitVo> pcsAlarmBitVos = iPcsAlarmBitService.queryList ( pcsAlarmBitVo );
            vo.setPcsAlarmBitVoList ( pcsAlarmBitVos );
        }
        return pcsVos;
    }

    @Override
    public Integer queryCount ( PcsVo pcsVo ) {
        Integer integer = pcsMapper.queryCount ( pcsVo );
        return integer;
    }


}
